<?php

class KitController
{
    private $kitAttachmentModel;
    private $kitBodyModel;
    private $kitListModel;

    function __construct()/*{{{*/
    {
        $this->kitAttachmentModel   =   new KitAttachmentModel();
        $this->kitBodyModel         =   new KitBodyModel();
        $this->kitListModel         =   new KitListModel();
    }/*}}}*/

    public function getKitList($kitInfo)/*{{{*/
    {
        $kitType    =   $this->checkKitType($kitInfo['kitType']);
        if (empty($kitType))    return  FALSE;

        if (empty($kitInfo['uid']))
        {
            AC::$errorNo    =   'KC003';
            return  FALSE;
        }
        $list   =   $this->kitListModel->getKitList($kitInfo);
        return  $list;
    }/*}}}*/

    public function getKitInfo($kitInfo)/*{{{*/
    {
        $kitType    =   $this->checkKitType($kitInfo['kitType']);
        if (empty($kitType))    return  FALSE;

        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KC007';
            return  FALSE;
        }
        $listInfo   =   $this->kitListModel->getKitListInfo($kitInfo);
        if (empty($listInfo))
        {
            AC::$errorNo    =   'KC004';
            return  FALSE;
        }
        $attachment =   $this->kitAttachmentModel->getKitAttachment($kitInfo);

        $bodyInfo   =   $this->kitBodyModel->getKitBody($kitInfo);
        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        $data['uid']        =   $kitInfo['uid'];
        $data['bodyId']     =   $kitInfo['bodyId'];
        $data['viewTimes']  =   1;
        $result =   $this->countTimes($data);

        return  array('bodyInfo' => $bodyInfo, 'attachment' => $attachment, 'listInfo' => $listInfo);
    }/*}}}*/

    public function addKit($kitInfo)/*{{{*/
    {
        $kitType    =   $this->checkKitType($kitInfo['kitType']);
        if (empty($kitType))    return  FALSE;

        if (empty($kitInfo['title'])  &&  empty($kitInfo['content'])    &&  empty($kitInfo['attachment']))
        {
            AC::$errorNo    =   'KC002';
            return  FALSE;
        }
        $kitId    =   $this->kitBodyModel->addKitBody($kitInfo);
        if (empty($kitId))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }

        $kitInfo['bodyId']=   $this->kitBodyModel->getBodyId($kitInfo, $kitId);
        if (empty($kitInfo['bodyId']))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }

        if (isset($kitInfo['attachment']) &&  count($kitInfo['attachment']) >   0)
        {
            $tmpInfo    =   array(
                                'uid'           =>  $kitInfo['uid'],
                                'bodyId'        =>  $kitInfo['bodyId'],
                                'attachment'    =>  $kitInfo['attachment'],
                                'kitType'       =>  $kitInfo['kitType'],
                            );
            $this->kitAttachmentModel->addKitAttachment($tmpInfo);
        }
        $kitListInsert=   $this->kitListModel->addKitList($kitInfo);
        if (empty($kitListInsert))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        #  feed & statistics    /*{{{*/
        # AC::$userStatistics[$kitInfo['uid']]['kitTimes']    =   1;
        AC::$feedInfo   =   array(
                                'uid'               =>  $kitInfo['uid'],
                                'eUid'              =>  AC::$hostUserBaseInfo['uid'],
                                'eNickname'         =>  AC::$hostUserBaseInfo['nickname'],
                                'authorUid'         =>  AC::$hostUserBaseInfo['uid'],
                                'authorNickname'    =>  AC::$hostUserBaseInfo['nickname'],
                                'bodyId'            =>  $kitInfo['bodyId'],
                                'type'              =>  $kitInfo['kitType'],
                            );
        !empty($kitInfo['content'])     &&  AC::$feedInfo['content']    =   $kitInfo['content'];
        !empty($kitInfo['title'])       &&  AC::$feedInfo['content']    =   $kitInfo['title'];
        empty(AC::$feedInfo['content']) &&  AC::$feedInfo['content']    =   ' write kit: '. $kitType. ', No title ? No content ? ! ';
        /*}}}*/

        return  array('bodyId' => $kitInfo['bodyId']);
    }/*}}}*/

    public function setKit($kitInfo)/*{{{*/
    {
        $kitType    =   $this->checkKitType($kitInfo['kitType']);
        if (empty($kitType))    return  FALSE;

        if (empty($kitInfo['title'])  &&  empty($kitInfo['content'])    &&  empty($kitInfo['attachment']))
        {
            AC::$errorNo    =   'KC006';
            return  FALSE;
        }
        $this->kitListModel->setKitList($kitInfo);
        $this->kitBodyModel->setKitBody($kitInfo);
        $this->kitAttachmentModel->setKitAttachment($kitInfo);
        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        return  TRUE;
    }/*}}}*/

    public function countTimes($kitInfo)/*{{{*/
    {
        $kitType    =   $this->checkKitType($kitInfo['kitType']);
        if (empty($kitType))    return  FALSE;

        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KC005';
            return  FALSE;
        }
        $result =   $this->kitListModel->countTimes($kitInfo);
        
        return  $result;
    }/*}}}*/

    public function removeKit($kitInfo)/*{{{*/
    {
        $kitType    =   $this->checkKitType($kitInfo['kitType']);
        if (empty($kitType))    return  FALSE;

        if (empty($kitInfo['uid']) || empty($kitInfo['bodyId']))
        {
            AC::$errorNo    =   'KC007';
            return  FALSE;
        }
        $this->kitListModel->removeKitList($kitInfo);
        $this->kitBodyModel->removeKitBody($kitInfo);
        $this->kitAttachmentModel->removeKitAttachment($kitInfo);
        if (!empty(AC::$errorNo))
        {
            UK::varDump(AC::$errorNo, __METHOD__);
            return  FALSE;
        }
        AC::$userStatistics[$kitInfo['uid']]['kitTimes']    =   -1;
        return  TRUE;
    }/*}}}*/

    private function checkKitType($kitType)/*{{{*/
    {
        $kitTypeArray   =   array(
                                AC::FEED_KIT_ANNIVERSARY    =>  'anniversary',
                                AC::FEED_KIT_BMI            =>  'bmi',
                                AC::FEED_KIT_MENSES         =>  'menses',
                                AC::FEED_KIT_NOTE           =>  'note',
                                AC::FEED_KIT_SCHEDULE       =>  'schedule',
                                AC::FEED_KIT_TALLYBOOK      =>  'tallybook',
                            );
        if (empty($kitTypeArray[$kitType]))
        {
            AC::$errorNo    =   'KC001';
            return  FALSE;
        }
        return  $kitType;
    }/*}}}*/

}

